package concurrency.chapter1;

import java.util.concurrent.TimeUnit;

/**
 * FileName: ThreadStatus
 * Author:   周壮
 * Date:     2020/5/17 17:26
 * Description:
 */
public class ThreadStatus {

    public static void main(String[] args) {

        // time_waiting
        new Thread(() -> {
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "time_waiting").start();

        // waiting , 线程 ThreadStatus 类锁上通过 wait 进行等待
        new Thread(() -> {
            while (true) {
                synchronized (ThreadStatus.class) {
                    try {
                        ThreadStatus.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "waiting").start();

        // 线程在 ThreadStatus 加锁后， 不会释放锁
        new Thread(new BlockedDemo(), "BlockDemo-01").start();
        new Thread(new BlockedDemo(), "BlockDemo-02").start();
    }
}
